using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace ImageReview
{
    public partial class PreviewItem : System.Web.UI.UserControl
    {
        // The image we're displaying		
        private TPDSControl.Image _image;
        public TPDSControl.Image Image
        {
            get
            {
                if (_image == null)
                {
                    _image = new TPDSControl.Image();
                }
                return _image;
            }
            set
            {
                _image = value;
            }
        }
        
        // Is this a barcode?
        public bool IsBarCodeImage = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ShowBarcodeFlag"] == null)
                Session["ShowBarcodeFlag"] = true;

            if (Session["ShowDeleteLink"] == null)
                Session["ShowDeleteLink"] = true;

            lnkBarcodeFlag.Visible = Convert.ToBoolean(Session["ShowBarcodeFlag"]);
            lnkDelete.Visible = Convert.ToBoolean(Session["ShowDeleteLink"]);
        }

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (Image.ThumbnailFilename != null)
			{
                // This is the url to the thumbnail image itself.
                thumbnailimage.Src = Image.ThumbnailFilename;
                if( IsBarCodeImage )
                    thumbnailimage.Alt = "barcode";

                // Assign an alternative image src via image handler to show larger images
                thumbnailimage.Attributes[ "altsrc" ] = String.Format( @"ImageHandler.ashx?file=~/{0}&Size=500", Image.Filename );
			}

			LinkButton lnkBarcodeFlag = (LinkButton)this.FindControl("lnkBarcodeFlag");

			if (Image.IsBarcode)
			{
				lnkBarcodeFlag.Text = "[not barcode]";
			}
			else
			{
				lnkBarcodeFlag.Text = "[barcode]";
			}

			if (Image.Filename != null)
			{
				Label lblFilename = (Label)this.FindControl("lblFilename");
				lblFilename.Text = Path.GetFileName(Image.Filename);
			}
		}
        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            try
            {
                Image.Delete(Server.MapPath(@"~/"));
                Image.DeleteFromImageset();
                Response.Redirect(Request.Url.ToString(), false);
            }
            catch (Exception ex)
            {
                Response.Write("<script type=\"text/javascript\">alert('"+ex.Message.Replace("'","").Replace(@"\",@"\\")+"');</script>");
            }
        }

        protected void lnkBarcodeFlag_Click(object sender, EventArgs e)
        {
            //set it to the opposite
            Image.SetAsBarcode(!Image.IsBarcode);
            Response.Redirect(Request.Url.ToString(), false);
        }

    }
}